package com.karashi.twitter.parser;

import java.io.StringReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.karashi.twitter.TwitterStatus;

public class TwitterParser {

	private static final String STATUSES = "statuses";
	private static final String STATUS = "status";
	private static final String TEXT = "text";
	private static final String SCREEN_NAME = "screen_name";
	private static final String PROFILE_IMAGE_URL = "profile_image_url";
	private static final String LANG = "lang";
	private static final String CREATED_AT = "created_at";

	private String urlStr;
	private String lang;

	public TwitterParser(String p_urlStr, String plang) {
		urlStr = p_urlStr;
		lang = plang;
	}

	public ArrayList<TwitterStatus> parse() {
		ArrayList<TwitterStatus> list = null;
		XmlPullParser parser = Xml.newPullParser();

		try {
			TwitterStatus currentStatus = null;

			byte[] data = HttpClient.getByteArrayFromURL(urlStr);

			String responseStr = new String(data, "UTF-8");
			parser.setInput(new StringReader(responseStr));

			int eventType = parser.getEventType();
			boolean isFinished = false;
			while (eventType != XmlPullParser.END_DOCUMENT && !isFinished) {
				String name = "";
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					list = new ArrayList<TwitterStatus>();
					break;

				case XmlPullParser.START_TAG:
					name = parser.getName();

					if (name.equalsIgnoreCase(STATUS)) {
						currentStatus = new TwitterStatus();
					} else if (currentStatus != null) {
						if (name.equalsIgnoreCase(TEXT)) {
							currentStatus.setText(parser.nextText());
						} else if (name.equalsIgnoreCase(SCREEN_NAME)) {
							currentStatus.setScreenName(parser.nextText());
						} else if (name.equalsIgnoreCase(PROFILE_IMAGE_URL)) {
							currentStatus.setProfileImageUrl(parser.nextText());
						} else if (name.equalsIgnoreCase(LANG)) {
							if (!lang.equalsIgnoreCase(parser.nextText())) {
								currentStatus = null;
							}
						} else if (name.equalsIgnoreCase(CREATED_AT)) {
							Date chk = currentStatus.getCreateAt();
							if (chk == null) {
								currentStatus.setCreateAt(formatCreateAt(parser.nextText()));
							}
						}
					}
					break;

				case XmlPullParser.END_TAG:
					name = parser.getName();
					if (name.equalsIgnoreCase(STATUS) && currentStatus != null) {
						list.add(currentStatus);
					} else if (name.equalsIgnoreCase(STATUSES)) {
						isFinished = true;
					}
					break;
				}
				eventType = parser.next();

			}

		} catch (Exception e) {
			// TODO: handle exception
		}

		return list;
	}

	private Date formatCreateAt(String nextText) {
		Date date = null;
		try {
			// localeをセットしないとパースされない
			DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
			format.setLenient(false);

			date = format.parse(nextText);
		} catch (ParseException e) {
			return null;
		}
		return date;
	}
}
